package com.wenshi.app.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import com.wenshi.app.activity.R;
import com.wenshi.view.IKeyValue;
import com.wenshi.view.IWsView;
import com.wenshi.view.WsVIewClickListener;
import com.wenshi.view.WsViewTools;




/**
 * 这是自定义的SwipeRefreshLayout
 */
public class WsSwipeRefreshAndLoadMoreLayout extends SwipeRefreshLayout implements IWsView {

    private String[] clicks;
    private float startY;
    private float startX;
    // 记录viewPager是否拖拽的标记
    private boolean mIsVpDragger;
    private int mTouchSlop;

    private final Context context;
    private IRefreshAndLoadMoreLisenter refreshAndLoadMoreLisenter;
    private TypedArray typedArray;

    @Override
    public void bindData(IKeyValue data) {


    }

    public void setWsLoadMoreListView(WsLoadMoreListView v) {

            mLoadMoreListView = v;
    }

    @Override
    public String[] getClick() {
        return new String[0];
    }

    @Override
    public void bindData(IKeyValue data, WsVIewClickListener listener) {
        bindData(data);
        clicks = WsViewTools.initAttrsByData((Activity) this.getContext(), data, clicks);
    }

    @Override
    public String getClassName() {
        return null;
    }

    public interface IRefreshAndLoadMoreLisenter {
        /**
         * 提供刷新的方法
         */
        public abstract void onRefresh();

        /**
         * 提供加载更多的方法
         */
        public abstract void onLoadMore();
    }

    private WsLoadMoreListView mLoadMoreListView;


    public WsSwipeRefreshAndLoadMoreLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        Resources res = getResources();
        //通过颜色资源文件设置进度动画的颜色资源
        setColorSchemeResources(
                android.R.color.holo_blue_light,
                android.R.color.holo_red_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_green_light);
        if (context instanceof IRefreshAndLoadMoreLisenter) {
           // Log.e("views", "IRefreshAndLoadMoreLisenter");
            refreshAndLoadMoreLisenter = (IRefreshAndLoadMoreLisenter) context;
        }
        typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.WsElement);
        //  typedArray = getContext().obtainStyledAttributes(attrs, WsViewTools.getWsAttrsIds(context));
        clicks = WsViewTools.initAttrs(this, context, typedArray);

        if (refreshAndLoadMoreLisenter != null)
            setOnRefreshListener(new OnRefreshListener() {
                @Override
                public void onRefresh() {
                    refreshAndLoadMoreLisenter.onRefresh();
                }
            });
    }


    /**
     * 触屏事件,如果ListView不为空且数据还在加载中，则继续加载直至完成加载才触摸此事件
     *
     * @param ev
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (mLoadMoreListView != null && mLoadMoreListView.isLoading()) {
            return false;
        }
        return super.onTouchEvent(ev);
    }

    /**
     * Sets refresh and load more lisenter.
     *
     * @param l the l
     */
    public void setRefreshAndLoadMoreLisenter(IRefreshAndLoadMoreLisenter l) {
        this.refreshAndLoadMoreLisenter = l;
    }

    //解决viewpager和下拉刷新的冲突
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                // 记录手指按下的位置
                startY = ev.getY();
                startX = ev.getX();
                // 初始化标记
                mIsVpDragger = false;
                break;
            case MotionEvent.ACTION_MOVE:
                // 如果viewpager正在拖拽中，那么不拦截它的事件，直接return false；
                if (mIsVpDragger) {
                    return false;
                }

                // 获取当前手指位置
                float endY = ev.getY();
                float endX = ev.getX();
                float distanceX = Math.abs(endX - startX);
                float distanceY = Math.abs(endY - startY);
                // 如果X轴位移大于Y轴位移，那么将事件交给viewPager处理。
                if (distanceX > mTouchSlop && distanceX > distanceY) {
                    mIsVpDragger = true;
                    return false;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                // 初始化标记
                mIsVpDragger = false;
                break;
        }
        // 如果是Y轴位移大于X轴，事件交给swipeRefreshLayout处理。
        return super.onInterceptTouchEvent(ev);
    }
}
